package com.woniu.service;

import com.woniu.adapter.HoldProductAdapter;
import com.woniu.adapter.ProductBuyAdapter;
import com.woniu.adapter.ProductSellAdapter;
import com.woniu.inlet.web.fo.ConfirmSellOrder;
import com.woniu.outlet.client.FinancePayFeign;
import com.woniu.outlet.client.SendOrderMsgToPayFeign;
import com.woniu.outlet.client.mq.FundAndGOldSellPayMQ;
import com.woniu.utils.NumberRegexValidationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 基金产品的实现
 */
@Slf4j
@Service
@Transactional
public class FundAndGoldService {

    @Autowired
    private HoldProductAdapter holdProductAdapter;

    @Autowired
    private ProductSellAdapter sellAdapter;

    @Autowired
    private ProductBuyAdapter buyAdapter;

    @Autowired
    private FinancePayFeign payFeign;

    @Autowired
    private SendOrderMsgToPayFeign sendOrderMsgToPayFeign;

    public Object fundHoldDetails(Long userId, String codeId) {
        return holdProductAdapter.fundHoldDetails(userId, codeId);
    }


    /**
     * 卖出基金或者黄金
     *
     * @param userId
     * @param codeId
     * @param sellShare
     */
    public String sellFundOrGold(String userId, String codeId, String sellShare, String payPassword) {
        if (!NumberRegexValidationUtil.isPositiveInteger(userId)
            || !NumberRegexValidationUtil.isPositiveIntegerOrDecimal(sellShare)) {
            return "参数非法";
        }
        int i = 1;
        boolean b = false;
        while (i <= 5) {
            try {
                b = payFeign.isPayPassword(Integer.valueOf(userId), payPassword);
                log.debug("feign支付密码成功");
                break;
            } catch (Exception e) {
                log.debug("卖出验证支付密码超时，重试第{}次", i);
                if (i == 5) {
                    log.debug("feign支付密码失败");
                    return "验证支付密码超时,请稍后再试";
                }
                i++;
            }
        }
        if (!b) {
            return "支付密码错误";
        }
        String fgAll = sellAdapter.sellFundOrGold(Long.valueOf(userId), codeId, sellShare);
        if (fgAll.equals("n")) {
            return "份额不足，不能卖出";
        }
        sendOrderMsgToPayFeign.sendMessage(FundAndGOldSellPayMQ.EXCHANGE, FundAndGOldSellPayMQ.ROUTING_KEY, fgAll);
        log.debug("要卖出的单子:{}", fgAll);
        return "y";
    }

    /**
     * 添加卖出订单的卖出确定时间
     *
     * @param sId
     * @param revocationDate
     */
    public void addRevocation(String sId, String revocationDate) {
        sellAdapter.addRevocation(sId, revocationDate);
    }

    /**
     * 撤销卖出订单
     *
     * @param sId
     */
    public String revocationSellOrder(String sId) {
        if(!NumberRegexValidationUtil.isPositiveInteger(sId)){
            return "invalid";
        }
        return sellAdapter.revocationOrder(sId);
    }

    /**
     * 支付模块卖出后确认金额等信息
     */
    public void confirmSellOrder(ConfirmSellOrder confirmSell) {
        sellAdapter.confirmSellOrder(confirmSell);
    }


    /**
     * 撤销买入订单
     *
     * @param bId
     * @return
     */
    public String revocationBuyOrder(String bId) {
        if(!NumberRegexValidationUtil.isPositiveInteger(bId)){
            return "invalid";
        }
        return buyAdapter.revocationOrder(bId);
    }
}
